package utils;


import error.FileErrorCode;
import error.FileServiceException;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * TODO
 * @author xj
 * @since 1.0
 */
@Slf4j
public class FileUploadUtil {



    public static String[] uploadFile(MultipartFile file) {
        String[] split = file.getOriginalFilename().split("\\.");
        try {
            return FileUtil.getStorageClient().upload_file(file.getBytes(), split[split.length - 1], null);
        }catch (ArrayIndexOutOfBoundsException e){
            throw new FileServiceException(FileErrorCode.FILE_FORMAT_IS_ERROR);
        }catch (IOException | MyException e) {
            log.error("文件上传失败:{}",e);
            throw new FileServiceException(FileErrorCode.FILE_UPLOAD_FAILE);
        }
    }
}
